import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/bean/bean_global.dart';
import 'package:flutter_learn_vi/src/bean/bean_mine_uestion.dart';
import 'package:flutter_learn_vi/src/common/global.dart';
import 'package:flutter_learn_vi/src/page/mine/question_model.dart';
import 'package:flutter_learn_vi/src/page/topic/topic_catar_select_page.dart';
import 'package:flutter_learn_vi/src/page/topic/topic_select_page.dart';
import 'package:flutter_learn_vi/src/utils/navigator_utils.dart';
import 'package:flutter_learn_vi/src/utils/toast_utils.dart';

import 'new/study_my_error_page.dart';

/// 创建人： Created by zhaolong
/// 创建时间：Created by  on 2021/1/5.
///
/// 可关注公众号：我的大前端生涯   获取最新技术分享
/// 可关注网易云课堂：https://study.163.com/instructor/1021406098.htm
/// 可关注博客：https://blog.csdn.net/zl18603543572
///
/// 代码清单
///代码清单
class HomeitemStudyWidget extends StatefulWidget {
  @override
  _HomeitemStudyWidgetState createState() => _HomeitemStudyWidgetState();
}

class _HomeitemStudyWidgetState extends State<HomeitemStudyWidget>
    with MineQuestionModel {
  @override
  void initState() {
    super.initState();
    initData();
  }

  StudyModelBean _studyModelBean;

  void initData() async {
    _studyModelBean = await getStudyModel();
    if (_studyModelBean != null) {
      rootStreamController.add(GlobalBean(204, true));
    }
  }

  @override
  Widget build(BuildContext context) {
    if (_studyModelBean == null) {
      return Container();
    }
    if (_studyModelBean.studyModelItemList.length < 4) {
      return Container();
    }
    return Container(
      height: 180,
      width: MediaQuery.of(context).size.width,
      child: GridView.builder(
        itemCount: 4,
        itemBuilder: (BuildContext context, int index) {
          List<StudyModelItemBean> list = _studyModelBean.studyModelItemList;
          StudyModelItemBean itemBean = list[index];
          return InkWell(
            child: buildClipRRect(itemBean),
            onTap: () {
              Widget page;
              if (itemBean.nodeType == "APPJUMP") {
                page = MyErrorPage();
              } else {
                if (itemBean.nodeValue != null) {
                  page = TopicSelectPage(
                    pageTitle: "${itemBean.nodeName}",
                    categoryOne: itemBean.nodeValue,
                  );
                } else if (itemBean.nodeValues != null &&
                    itemBean.nodeValues.length > 0) {
                  page = TopicCatarSelectTwoPage(
                      pageTitle: "${itemBean.nodeName}",
                      nodeValues: itemBean.nodeValues);
                } else {
                  //暂无详情
                  ToastUtils.showToast("暂无详情");
                  return;
                }
              }
              NavigatorUtils.pushPage(
                context,
                page,
              );
            },
          );
        },
        gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
          //纵轴间距
          mainAxisSpacing: 2.0,
          //横轴间距
          crossAxisSpacing: 2.0,
          //子组件宽高长度比例
          childAspectRatio: 1.1, crossAxisCount: 4,
        ),
      ),
    );
  }

  ClipRRect buildClipRRect(StudyModelItemBean itemBean) {
    return ClipRRect(
      child: Container(
        margin: EdgeInsets.only(left: 1, right: 1, bottom: 4),
        child: Column(
          children: [
            Container(
              margin: EdgeInsets.only(left: 4, right: 4),
              padding: EdgeInsets.all(10),
              decoration: BoxDecoration(
                  color: Colors.white,
                  borderRadius: BorderRadius.all(
                    Radius.circular(8),
                  )),
              child: ClipOval(
                child: itemBean.nodeLogo != null
                    ? CachedNetworkImage(
                        imageUrl: itemBean.nodeLogo,
                        width: 26,
                      )
                    : Image.asset(
                        "assets/images/2.0x/app_icon.png",
                      ),
              ),
            ),
            Container(
              margin: EdgeInsets.only(top: 4),
              child: Text(
                "${itemBean.nodeName}",
                style: TextStyle(
                    fontWeight: FontWeight.w500,
                    color: Colors.blueGrey,
                    fontSize: 14),
              ),
            ),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
  }
}
